package com.immortal.core.autoconfigure;

import com.immortal.core.utils.SpringUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

/**
 * <p>TODO
 *
 * @author wind
 * @version 1.0
 * @since 2018/12/12
 */
@Configuration
public class WebMvcAutoConfiguration implements WebMvcConfigurer {
    @ConditionalOnMissingBean
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor localeChangeInterceptor= new LocaleChangeInterceptor();
        localeChangeInterceptor.setParamName("lang");
        return localeChangeInterceptor;
    }
    @ConditionalOnMissingBean
    @Bean
    public CookieLocaleResolver localeResolver() {
        return new CookieLocaleResolver();
    }

    @ConditionalOnMissingBean(CookieLocaleResolver.class)
    @Bean("localResolver")
    public SessionLocaleResolver sessionLocaleResolver() {
        return new SessionLocaleResolver();
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        LocaleChangeInterceptor localeChangeInterceptor = SpringUtil.getBean(LocaleChangeInterceptor.class);
        if (localeChangeInterceptor != null) {
            registry.addInterceptor(localeChangeInterceptor);
        }
    }
}
